#include "PostalCode.h"


PostalCode::PostalCode(){

}
PostalCode::PostalCode(unsigned short reference, unsigned short indicative, City & city){
    setReference(reference);
    setIndicative(indicative);
    setCity(city);
}


PostalCode::~PostalCode(){

}

/**
 * Sets PostalCode's reference number. Must be within 0 and 9999. If not an
 * InvalidPostalCode exception is thrown
 * @return
 */
unsigned short PostalCode::getReference() const{
    return reference;
}

void PostalCode::setReference(unsigned short reference){
    if(reference<1000|| reference > 9999)
        throw InvalidPostalCode();

    this->reference = reference;
}

unsigned short PostalCode::getIndicative() const{
    return indicative;
}

/**
 * Sets PostalCode's indicative number. Must be within 0 and 999. If not an
 * InvalidPostalCode exception is thrown
 * @param indicative
 */
void PostalCode::setIndicative(unsigned short indicative){
    if(indicative<0 || indicative > 999)
        throw InvalidPostalCode();

    this->indicative = indicative;
}


City *PostalCode::getCity() const{
    return city;
}


void PostalCode::setCity(City &city){
    this->city = &city;
}

string PostalCode::printPostalCode() {
    stringstream info;

    info << this->getReference() << " - " << this->getIndicative()
            << "\t" << this->getCity().printCity() << endl;
    
    return info.str();
}

void PostalCode::newAddress(PostalCode & postalCode) {
    bool error = true;

    while(error) {
        error = false;
        
        try {
            unsigned short reference, indicative;
			
            cout << "Reference: ";
            cin >> reference;
            
            cout << endl << "Indicative: ";
            cin >> indicative;

            cout << endl << "City: "
            city;

            postalCode.setIndicative(reference);
            postalCode.setReference(indicative);
        }
        catch(PostalCode::InvalidPostalCode x) {
            x.printError(cerr);
            error = true;
        }
    }
}
